import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '科室',		name: 'keyword' },
			{ label: '部门',		name: 'dept_id',	type: 'select', loading:true, nameStr:'depart_name', idStr:'uuid' },
			{ label: '状态',		name: 'enabled',	type: 'select', loading:true },
		],
		// 提交表单列表
		formData : [
			{ label: '科室名称',		name: 'project_name', 	full:true, required:true },
			{ label: '科室编号',		name: 'project_code', 	half:true, required:true, disabled:true },
			{ label: '中文简称',		name: 'short_name', 	half:true },
			{ label: '英文名称',		name: 'en_name', 		half:true },
			{ label: '英文简称',		name: 'short_en_name', 	half:true },
			{ label: '关联部门',		name: 'dept_id', 		half:true, required:true, type: 'select', nameStr:'depart_name', idStr:'uuid' },
			{ label: '排序',			name: 'sort', 			half:true, required:true, type: 'number', nameStr:'group_name' },
			{ label: '是否外包',		name: 'is_commission', 	half:true, required:true, type: 'radio',  data:[{name:'是',value:'1'},{name:'否',value:'0'}] },
		]
	}
	
	model = {}
	query = { pid: 0 }
	componentDidMount(){
		// 获取上级页面参数
		const { uuid } =  $fn.query()
		if(uuid){ 
			this.query = { pid:0, dept_id: uuid }
			this.uuid = uuid
		}
		//
		this.fetch()
	}
	// 获取下拉数据
	getSelect = async () => {
		const { forms, formData } = this.state
		$cache.then( async f => {
			// 部门
			$fn.setRows([forms,formData], 'dept_id', await f.getCache({ api:'department' } ) )
			// 回显部门
			if(this.uuid){
				this.searchFormRef.setValue({ dept_id: this.uuid  })
				this.formRef.setValue({ dept_id: this.uuid  })
			}
			// 状态
			const enabledData = $fn.setRow(forms, 'enabled', await f.company_state )
			//
			this.setState({ forms, enabledData })
		})
		
	}
	// paging
	fetch = () => $fn.fetch.call(this,'project-team/index')
	
	// 表单
	cols = [
		{ title: '科室编号',		dataIndex: 'project_code', 		width:80, align:'center' },
		{ title: '科室名称', 	dataIndex: 'project_name', 		width:150 },
		{ title: '简称', 		dataIndex: 'short_name', 		width:100 },
		{ title: '英文名称', 	dataIndex: 'en_name', 			width:150 },
		{ title: '英文简称', 	dataIndex: 'short_en_name', 	width:100 },
		{ title: '所属部门', 	dataIndex: 'depart_name', 		width:150 },
		{ title: '所属公司', 	dataIndex: 'company_name', 		width:150 },
		{ title: '状态', 		dataIndex: 'enabled', 			...$fn.table.status, align:'center', render: text => $fn.getState(this.state.enabledData,text)},
		TableControls.controls.call(this, { width:235, align:'left'})
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled, id }) => {
		return [
			// 编辑
			TableControls.edit.call(this, 'project-team/info', { uuid }, { ids:({ result_model })=>({ result_model }), click:()=>{
				const { formData } = this.state
				$fn.hideRows(formData, 'project_code', false)
				$fn.disabledRows(formData, ['project_name','project_code','is_commission'], true)
			} }),			
			
			(+enabled === -1 || +enabled === 0) && this.onEnabled( 1,  [ uuid ] ),	// 启用
			(+enabled === 1  || +enabled === 0) && this.onEnabled( 0,  uuid ),		// 禁用
			
			TableControls.pushTo.call(this, '查看', `post-manage?uuid=${id}`),
		]
	}
	// 操作按钮
	controls = () => [
		Controls.add.call(this, { click: ()=>{
			const { formData } = this.state
			$fn.hideRows(formData, 'project_code', true)
			$fn.disabledRows(formData, ['project_name', 'project_code', 'is_commission'], false)
			this.setState({ formData })
		}}),			// 添加
		this.onEnabled(1),	// 启用
		this.uuid && Controls.back.call(this)
	]
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'project-team/open', off:'project-team/del', isKeys: !uuid, onComplete:()=>{
		$fn.dataRemove('project-team/selectMenu')
		$fn.resetMenu()
	} })
	
	render(){
		return (
			<>
				<Page title='科室管理' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this, { onForm: this.getSelect }) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'project-team/addProject', editApi:'project-team/edit', onComplete:({ add })=>{
					$fn.dataRemove('project-team/selectMenu')
				} }) }
			</>
		)
	}
}

export default Index